﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace NorthwindSample.Data
{
    public static class EmployeesRepository
    {
        public static IList<Employee> GetEmployees()
        {
            NorthwindEntities northwind = new NorthwindEntities();
            return northwind.Employees.ToList();
        }

        public static bool SaveEmployee(Employee employee)
        {
            NorthwindEntities northwind = new NorthwindEntities();
            if (employee.EmployeeID > 0)
            {
                Employee persistedEmployee = northwind.Employees.First(e => e.EmployeeID == employee.EmployeeID);
                persistedEmployee.Copy(employee);
            }
            else
            {
                northwind.AddToEmployees(employee);
            }
            return northwind.SaveChanges() == 1;
        }

        public static bool DeleteEmployee(int employeeID)
        {
            NorthwindEntities northwind = new NorthwindEntities();
            Employee employee = northwind.Employees.First(e => e.EmployeeID == employeeID);
            northwind.DeleteObject(employee);
            return northwind.SaveChanges() == 1;
        }
    }
}
